package com.codeoss.module.stock.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.stock.dao.StockOutLineMapper;
import com.codeoss.module.stock.model.StockOutLine;
import com.codeoss.module.stock.service.IStockOutLineService;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service
public class StockOutLineService extends BaseService<StockOutLineMapper, StockOutLine> implements IStockOutLineService {
	@Autowired
    private IUomUomConversionService uomUomConversionService;
	@Transactional
	public void insert(StockOutLine stockOutLine) {
		beforeSave(stockOutLine, true);
		super.insert(stockOutLine);
	}
	@Transactional
	public void update(StockOutLine stockOutLine) {
		beforeSave(stockOutLine, false);
		super.update(stockOutLine);
	}
	@Transactional
	protected void beforeSave(StockOutLine stockOutLine, boolean isNewRecord) {
		Long productId=stockOutLine.getProductProductId();
	    BigDecimal qtyEntered=stockOutLine.getQtyEntered();
	    Long uomUomId=stockOutLine.getUomUomId();
	    if(productId!=null&&qtyEntered!=null&& uomUomId!=null) {
	    	BigDecimal movementQty=uomUomConversionService.convert(productId, uomUomId, qtyEntered);
	    	stockOutLine.setMovementQty(movementQty);
	    }
	}
	
	@Override
	public List<StockOutLine> getLineListByStockOutId(Long stockOutId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockOutLine> queryWrapper=new QueryWrapper<StockOutLine>();
		queryWrapper.eq("stock_out_id", stockOutId);
		return list(queryWrapper);
	}
}